#version 450 compatibility




layout(local_size_x = 8, local_size_y = 1, local_size_z = 1) in;


layout( binding = 0, rgba8ui) uniform uimage2D Pix;

layout( binding = 1 ) uniform Buf_LoalParameter1 {
    ivec2   m_Offset;
    ivec2   m_WidgetOffset;
    uvec2   m_Color;
    uint    m_DataNum;
    uint    m_Channel;

    float   m_SamplingGap;
    float   m_Gain;
    float   m_LineMaxWidth;
};

layout( binding = 2 ) buffer Buf_LoalParameter2 {
    float   m_Intensity[];
};



void main() {
    //gl_NumWorkGroups.y;
    const uint localID = gl_LocalInvocationID.x;
    if(localID >= m_Channel) return;

    float height_min = 0.0;
    float height_max = 0.0;
    for (uint i=0; i<uint(m_SamplingGap); ++i) {
        float intensity = m_Intensity[int(m_SamplingGap*float(gl_WorkGroupID.x*m_Channel)) + i];
        //height_min = min(height_min, intensity);
        height_max = max(height_max, abs(intensity));
    }

    int offset = int(gl_NumWorkGroups.y / m_Channel);

    //height_min /= offset;
    //height_max /= offset;
    height_min /= 32767.0;
    height_max /= 32767.0;
    
    height_min *= m_Gain;
    height_max *= m_Gain;
    //height_min = float(offset) / height_min;
    //height_max = float(offset) / height_max;
    //height_min *= offset;
    //height_max *= offset;

    
    height_min *= float(offset);
    height_max *= float(offset);

    
    offset *= int(localID+1);
    offset /= 2;
    height_min += offset;
    height_max += offset;
    




    ivec2 coord = ivec2(gl_WorkGroupID.x, gl_WorkGroupID.y);
    if(coord.y > height_min && coord.y < height_max){
        uvec4 c = uvec4(bitfieldExtract(m_Color.x, 0, 8), 
                        bitfieldExtract(m_Color.x, 8, 8), 
                        bitfieldExtract(m_Color.x, 16, 8), 
                        bitfieldExtract(m_Color.x, 24, 8));
        
        imageStore(Pix, coord, c);
    }
    

}



